
import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入组件
// import Login from '@/views/Login'
// import Layout from '@/views/Layout'
// import Home from '@/views/Home'
// import User from '@/views/User'
// import  Search  from '@/views/Search'
// import  searchResult  from '@/views/Search/searchResult'
// import ArticleDetail from '@/views/ArticleDetail'
// import UserEdit from '../views/User/uesrEdit.vue'

/**
 * 路由懒加载的好处，提高第一个页面的加载速度，webpack 在打包的时候，不会一次性的把所有的import 都打包
 * 把组件对应的js分成了若干个js ，路由切换到哪个页面，才去加载对应的 js文件，
 */
import {getToken} from '@/utils/token'


Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/layout/home' //默认显示 home的首页
  },
  {
    path:'/login',
    // component: Login
    component:()=> import(/*webpackChunkName: "Login" */ '@/views/Login')

  },
  {
    path:'/layout',
    // component: Layout,
    component:()=> import(/*webpackChunkName: "Layout" */ '@/views/Layout'),
    children:[
      {
        path:'home',
        // component:Home
        component:()=> import(/*webpackChunkName: "Home" */ '@/views/Home'),
        meta:{
          ScrollT:0 //保存首页离开时候的位置
        }
      },
      {
        path:'user',
        // component:User,
        component:()=> import(/*webpackChunkName: "User" */ '@/views/User')
      }
    ]
  },
  {
    path:'/search',
    // component:Search
    component:()=> import(/*webpackChunkName: "Search" */ '@/views/Search')
  },
  {
    path:'/search/searchResult:kw',
    // component:searchResult
    component:()=> import(/*webpackChunkName: "searchResult" */ '@/views/Search/searchResult')
  },
  {
    path:'/articledetail',
    // component:ArticleDetail
    component:()=> import(/*webpackChunkName: "ArticleDetail" */ '@/views/ArticleDetail')
  },
  {
    path:'/userEdit', 
    // component:UserEdit
    component:()=> import(/*webpackChunkName: "UserEdit" */ '@/views/User/uesrEdit')
  }
  
]



const router = new VueRouter({
  routes
})
//路由前置守卫
router.beforeEach((to,from,next) =>{
  //如果用户已经登录了，就不让他回到登录页面
  if(getToken() && to.path==='/login'){
    
    // next(false)
    next('/layout/home')
  }else{
    next()
  }
})

export default router
